package com.fengba.nanzhi.tabbar04.wz;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.fengba.nanzhi.fengba.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;

/**
 * Created by 89311 on 2018/4/25.
 */

public class PayMentAdapter extends RecyclerView.Adapter<PayMentAdapter.LinerViewHolder> {
    private Context context;
    private JSONArray jsonArray;
    private String money;
    private String money1;

    public PayMentAdapter(Context context,JSONArray jsonArray,String money,String money1) {
        this.context = context;
        this.jsonArray = jsonArray;
        this.money = money;
        this.money1 = money1;
    }

    @Override
    public PayMentAdapter.LinerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PayMentAdapter.LinerViewHolder(LayoutInflater.from(context).inflate(R.layout.wz_pay_ment_item_view,parent,false));
    }

    @Override
    public void onBindViewHolder(final PayMentAdapter.LinerViewHolder holder, final int position) {
        try {
            final JSONObject jsonObject = (JSONObject) jsonArray.get(position);

            holder.kjName.setText(jsonObject.get("tunnelname").toString());
            if(jsonObject.get("cleartype").toString().trim().equals("1")) {
                holder.mode.setText("自动结算");
                holder.mode.setTextColor(Color.parseColor("#1155ea"));
                //提示
                holder.PayMentTime.setText("(交易时间)"+jsonObject.get("start_time").toString()+" - "+jsonObject.get("end_time").toString()+"支付成功后银行系统自动次日结算");
            }else{
                holder.mode.setText("手动结算");
                holder.mode.setTextColor(Color.parseColor("#FF9600"));
                //提示
                holder.PayMentTime.setText("(交易时间)"+jsonObject.get("start_time").toString()+" - "+jsonObject.get("end_time").toString()+"支付成功后实现手动提现");
            }
            //单日最大限额
            double max_money = Double.parseDouble(jsonObject.get("day_trading").toString());
            holder.PayMentMax.setText(holder.PayMentMax.getText().toString()+max_money/10000+"万");

            //费率保留两位消失
            double min_rate = Double.parseDouble(jsonObject.get("rate").toString());
            DecimalFormat df = new DecimalFormat("0.00");
            String rate = df.format(min_rate);
            holder.PayMentRate.setText(rate);

            //设置点击事件
            holder.myview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String tunnel_code="";
                    try {
                        tunnel_code = (String) ((JSONObject)jsonArray.get(position)).get("code");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Intent intent = new Intent(context,ElementActivity.class);
                    intent.putExtra("kjName",holder.kjName.getText().toString());
                    intent.putExtra("money",money);
                    intent.putExtra("money1",money1);
                    intent.putExtra("tunnel_code",tunnel_code);
                    context.startActivity(intent);
                }
            });


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        if(jsonArray!=null){
            return jsonArray.length();
        }else{
            return 0;
        }
    }

    class LinerViewHolder extends RecyclerView.ViewHolder{
        private View myview;
        private TextView kjName,mode,PayMentMax,PayMentRate,PayMentTime;

        public LinerViewHolder(View itemView) {
            super(itemView);
            myview = itemView;
            kjName = itemView.findViewById(R.id.kjName);
            mode = itemView.findViewById(R.id.mode);
            PayMentMax = itemView.findViewById(R.id.PayMentMax);
            PayMentRate = itemView.findViewById(R.id.PayMentRate);
            PayMentTime = itemView.findViewById(R.id.PayMentTime);
        }
    }
    public static void showToast(final Activity ctx, final String msg) {
        // 判断是在子线程，还是主线程
        if ("main".equals(Thread.currentThread().getName())) {
            Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
        } else {
            // 子线程
            ctx.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }


}
